// pages/questionnaire/questionnaire.js
const APP = getApp();
import API from "../../api/Api.js"
import constants from "../../assets/constants.js"
import common from "../../assets/common.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userSurveyId: "",
    surveyName: "",
    QUESTION_TYPE: {},
    questions: [],
    select: [],
    selectIndexMap: [],
    userSurveyScoreVos: [],
    isAnalyseShow: false,
    userSurveyVo: {}
  },

  submitSurvey(e) {
    console.log("submitSurvey");
    this.doneSurvey();
  },

  closeAnalyseShow(e) {
    this.setData({
      isAnalyseShow: false
    });
  },

  onRadioChange(e) {
    let _that = this;
    let index = _that.data.selectIndexMap[e.detail.name];
    _that.setData({
      ['select[' + index + ']']: e.detail.value
    });
  },
  formSubmit(e) {
    let _that = this;
    let needReturn = false;
    let list = [];
    console.log(_that.data.select);
    let notDoneIndex = "";
    _that.data.select.forEach((v, k) => {
      if(!v) {
        notDoneIndex += k + 1 + ",";
        needReturn = true;
      }
    });
    if (needReturn) {
      let str = "第" + notDoneIndex.substring(0, notDoneIndex.length - 1) + "项未填选";
      common.showToast("cancel", str);
      return;
    }
    for (let i in e.detail.value) {
      let arr = i.split(",,");
      list.push({
        questionVo: {
          id: arr[1]
        },
        answer: e.detail.value[i],
        type: arr[0]
      });
    }
    _that.makeUserSurveyVo(list);
    _that.getCalculateSurveyResultGrades();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _that = this;
    _that.data.userSurveyId = options ? options.usersurveyid : "";
    _that.setData({
      surveyName: options.surveyname,
      QUESTION_TYPE: constants.QUESTION_TYPE
    });
    console.log("---------------load---------------");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let _that = this;
    _that.getQuestionnaire();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("---------------unload---------------");
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取问卷
   */
  getQuestionnaire() {
    let _that = this;
    _that.showLoading();
    console.log("getQuestionnaire");
    console.log("userSurveyId:" + _that.data.userSurveyId);
    console.log("userVo.id:" + APP.globalData.userVo.id);
    API.questionnaire.getQuestionnaire({
      userId: APP.globalData.userVo.id,
      userSurveyId: _that.data.userSurveyId
    }).then(res => {
      console.log("res:");
      console.log(res);
      if (!res) {
        _that.setData({
          questions: []
        });
        _that.hideLoading();
        return;
      }
      //因为setData不允许select[String]，所以采用的select[index]
      _that.data.select = [];
      res.questions.forEach((v, k) => {
        if (v.type === _that.data.QUESTION_TYPE.SINGLE) {
          _that.data.select[k] = "";
          _that.data.selectIndexMap[v.type + ",," + v.id] = k;
        }
      });
      _that.setData({
        questions: res.questions ? res.questions : []
      });
      _that.hideLoading();
    }).catch(() => {
      _that.hideLoading();
    });
  },

  makeUserSurveyVo(list) {
    let _that = this;
    let userSurveyVo = {
      id: _that.data.userSurveyId,
      userVo: APP.globalData.userVo,
      userSurveyResultVos: list
    };
    _that.data.userSurveyVo = userSurveyVo;
    return userSurveyVo;
  },

  /**
   * 获得评分
   */
  getCalculateSurveyResultGrades() {
    let _that = this;
    _that.showLoading();
    API.survey.getCalculateSurveyResultGrades(_that.data.userSurveyVo).then(res => {
      _that.hideLoading();
      if (!res) {
        _that.setData({
          isAnalyseShow: false,
          userSurveyScoreVos: []
        });
        return;
      }
      _that.setData({
        isAnalyseShow: true,
        userSurveyScoreVos: res
      });
    }).catch(() => {
      _that.hideLoading();
    });
  },

  /**
   * 完成问卷
   */
  doneSurvey() {
    let _that = this;
    _that.showLoading();
    API.survey.doneSurvey(_that.data.userSurveyVo).then(res => {
      _that.hideLoading();
      if (!res) {
        common.showToast("forbidden", "提交失败");
        return;
      }
      common.showToast("success", "提交成功", () => {
        wx.navigateBack({
          delta: 2
        });
      });
    }).catch(() => {
      _that.hideLoading();
    });
  },

  /**
   * 显示加载
   */
  showLoading() {
    let _that = this;
    wx.showLoading({
      title: '加载中',
    });
  },

  /**
   * 隐藏加载
   */
  hideLoading() {
    let _that = this;
    wx.hideLoading();
  }
})